import { addDepartment, deleteDepartment, queryDepartment, addWardApi, deleteWardApi, queryWardApi } from "@/Api/Department";
import EventBus from "@/utils/EventBus";
import { defineStore } from "pinia";

import { useCommon } from "./Common";

const CommonStore = useCommon()

export const useDepartment = defineStore("department", {
    state: (): departmentState => {
        return {
            loading: false,
            searchForm: {
                department: '' as string | null,
                illArea: '' as string | null,
                departmentWard: [],
                current: 1,
                pageNo: 1,
                pageSize: 20,
                total: 0,
                pageSizeOptions: ['20', '30', '50', '100'],
                showSizeChanger: true,
                showTotal(total: number) { return `共${total}条` },
                onChange(pageNo: number, pageSize: number) {
                    this.current = pageNo
                    this.pageNo = pageNo
                    this.pageSize = pageSize
                }
            },
            tableData: [],
            addForm: {} as illAreaOrDepartment
        };
    },
    actions: {
        async getTableData() {
            this.loading = true
            if (this.searchForm.departmentWard != undefined && this.searchForm.departmentWard.length == 1) {
                this.searchForm.department = this.searchForm.departmentWard[0]
                this.searchForm.illArea = ''
            } else if (this.searchForm.departmentWard != undefined && this.searchForm.departmentWard.length == 2) {
                this.searchForm.department = this.searchForm.departmentWard[0]
                this.searchForm.illArea = this.searchForm.departmentWard[1]
            } else {
                if (CommonStore.department != '检验科') {
                    this.searchForm.department = CommonStore.department
                    this.searchForm.illArea = CommonStore.illArea
                } else {
                    this.searchForm.department = ''
                    this.searchForm.illArea = ''
                }
            }

            try {
                Promise.all([
                    queryDepartment(this.searchForm),
                    queryWardApi(this.searchForm)
                ]).then((result: any[]) => {
                    console.log(result);
                    result[0].forEach((item: any) => {
                        item.illArea = ''
                    })
                    this.tableData = result[0].concat(result[1])
                    this.tableData.forEach((item: any, index) => {
                        item.index = index + 1
                        if (item.illArea) {
                            item.departmentWard = `${item.department}-${item.illArea}`
                        } else {
                            item.departmentWard = item.department
                        }
                    })
                    this.searchForm.total = this.tableData.length
                })
            } finally {
                this.loading = false
            }

            try {
                Promise.all([CommonStore.getAllFirstData(), CommonStore.getAllSecondData()])
                    .then(() => {
                        return CommonStore.handleSecondData()
                    }).then((res) => {
                        return CommonStore.handleThirdData()
                    })
            } finally {
            }
        },
        async getDepartment() {
            this.loading = true
            if (this.searchForm.departmentWard != undefined && this.searchForm.departmentWard.length == 1) {
                this.searchForm.department = this.searchForm.departmentWard[0]
                this.searchForm.illArea = ''
            } else if (this.searchForm.departmentWard != undefined && this.searchForm.departmentWard.length == 2) {
                this.searchForm.department = this.searchForm.departmentWard[0]
                this.searchForm.illArea = this.searchForm.departmentWard[1]
            } else {
                if (CommonStore.department != '检验科') {
                    this.searchForm.department = CommonStore.department
                    this.searchForm.illArea = CommonStore.illArea
                } else {
                    this.searchForm.department = ''
                    this.searchForm.illArea = ''
                }
            }

            try {
                const data = await queryDepartment(this.searchForm) as any
                if (data) {
                    this.tableData = data
                    this.tableData.forEach((item: any, index) => {
                        item.index = index + 1
                    })
                    this.searchForm.total = this.tableData.length
                }
            } finally {
                this.loading = false
            }
        },
        async getWardList() {
            this.loading = true
            if (this.searchForm.departmentWard != undefined && this.searchForm.departmentWard.length == 1) {
                this.searchForm.department = this.searchForm.departmentWard[0]
                this.searchForm.illArea = ''
            } else if (this.searchForm.departmentWard != undefined && this.searchForm.departmentWard.length == 2) {
                this.searchForm.department = this.searchForm.departmentWard[0]
                this.searchForm.illArea = this.searchForm.departmentWard[1]
            } else {
                if (CommonStore.department != '检验科') {
                    this.searchForm.department = CommonStore.department
                    this.searchForm.illArea = CommonStore.illArea
                } else {
                    this.searchForm.department = ''
                    this.searchForm.illArea = ''
                }
            }
            try {
                const data = await queryWardApi(this.searchForm) as any
                if (data) {
                    this.tableData = data
                    this.tableData.forEach((item: any, index) => {
                        item.index = index + 1
                    })
                    this.searchForm.total = this.tableData.length
                }
            } finally {
                this.loading = false
            }
        },
        async addDepartment() {
            const data = await addDepartment(this.addForm)
            if (!data) {
                EventBus.emit('AddSuccess')
                this.getTableData()
            }
        },
        async delDepartment(id: string | number) {
            const data = await deleteDepartment({ id })
            if (!data) {
                EventBus.emit('DeleteSuccess')
                this.getTableData()
            }
        },
        async addWard() {
            const data = await addWardApi(this.addForm)
            if (!data) {
                EventBus.emit('AddSuccess')
                this.getTableData()
            }
        },
        async delWard(id: number) {
            const data = await deleteWardApi({ id })
            if (!data) {
                EventBus.emit('DeleteSuccess')
                this.getTableData()
            }
        }
    },
});